package ru.cjay.loading{

import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.system.Security;
import flash.system.LoaderContext;
import flash.utils.ByteArray;


public class ImgLoader extends Sprite {

    private var This:*;
    private var picPath:String;
    private var pLoader:Loader;
    private var picTarget:MovieClip;
    private var loadedBitmap:*;
    private var BD:BitmapData;

    public function ImgLoader(path:String, targetMC:MovieClip) {
        This = this;
        picPath = path;
        picTarget = targetMC;

        pLoader = new Loader();
        configureListeners(pLoader.contentLoaderInfo);
        var request:URLRequest = new URLRequest(picPath);

        var loaderContext:LoaderContext = new LoaderContext();
        loaderContext.checkPolicyFile = true;

        pLoader.load(request, loaderContext);
    }

    private function configureListeners(dispatcher:IEventDispatcher):void {
        dispatcher.addEventListener(Event.COMPLETE, completeHandler);
        dispatcher.addEventListener(Event.INIT, imageInited);
        dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
        dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
    }

    public function imageInited(event:Event):void {
        trace("pLoader.content.loaderInfo=" + pLoader.content.loaderInfo);
        //tmpLoader.loadBytes(pLoader.content.loaderInfo)
        pLoader.content as Bitmap;
        loadedBitmap = pLoader.content;

        trace("picture from " + pLoader.contentLoaderInfo.url);
        resizePicture()
    }

    private function completeHandler(event:Event):void {
        //trace("PicLoader: completeHandler " + event);
    }

    private function httpStatusHandler(event:HTTPStatusEvent):void {
        //	trace("PicLoader: httpStatusHandler " + event);
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        //trace("PicLoader: ioErrorHandler " + event);
    }

    private function progressHandler(event:ProgressEvent):void {
        //trace("PicLoader:  bytesLoaded=" + Math.round(event.bytesLoaded/event.bytesTotal*100)+"%");
    }

    private function resizePicture() {
        var delitel = 0;
        if (loadedBitmap.width > picTarget.width) {
            delitel = loadedBitmap.width / picTarget.width;
            loadedBitmap.width = loadedBitmap.width / delitel;
            loadedBitmap.height = loadedBitmap.height / delitel;
        }

        if (loadedBitmap.height > picTarget.height) {
            delitel = loadedBitmap.height / picTarget.height;
            loadedBitmap.height = loadedBitmap.height / delitel;
            loadedBitmap.width = loadedBitmap.width / delitel;
        }
        loadedBitmap.x = (picTarget.width - loadedBitmap.width) / 2;
        loadedBitmap.y = (picTarget.height - loadedBitmap.height) / 2;
        //loadedBitmap.alpha=0.3;
        picTarget.addChild(loadedBitmap);
        BD = new BitmapData(picTarget.width, picTarget.height, true, 0x000000);
        BD.draw(picTarget);
        loadedBitmap.alpha = 0;
        picTarget.parent.dispatchEvent(new CustomEvent("previewLoaded", BD, null, null));
    }
}

}

import flash.display.BitmapData;
import flash.events.Event;

class CustomEvent extends Event{
    public var bd:BitmapData;
    function CustomEvent(type:String, p_bd:BitmapData,  bubbles:Boolean = false, cancelable:Boolean = false) {
        bd = p_bd;
        super(type, bubbles, cancelable);
    }
}